﻿using System;
using Microsoft.Win32;
using System.Diagnostics;

namespace wpf_print.utils
{
    internal class SelfStart
    {

        /// <summary>
        /// 将本程序设shell
        /// </summary>
        /// <returns></returns>
        public static bool SetMeStart()
        {
            string appName = "testPrint";
            string appPath = Process.GetCurrentProcess().MainModule.FileName;
            try
            {

                // 增加 shell注册 用于浏览器调取
                RegistryKey classRoot = Registry.ClassesRoot;
                RegistryKey classKey = classRoot.OpenSubKey(appName + @"\shell\open\command", true);

                if (classKey == null)
                {
                    classRoot.CreateSubKey(appName + @"\shell\open\command");
                    classKey = classRoot.OpenSubKey(appName + @"\shell\open\command", true);
                }
                classKey.SetValue("", appPath + " %1");
                classKey = classRoot.OpenSubKey(appName, true);
                classKey.SetValue("URL Protocol", appPath);
                classKey.Close();
        }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }
}
